package com.anyisn.anyflickr.modules.PhotosSlide.view
{
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.common.events.PhotoLoadEvent;
	import com.anyisn.anyflickr.modules.PhotosSlide.PhotosSlideModuleFacade;
	import com.anyisn.anyflickr.modules.PhotosSlide.view.component.PhotosSlideWindow;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	public class PhotosSlideWindowMediator extends Mediator
	{
		public static const NAME:String = 'PhotosSlideWindowMediator';
		
		public function PhotosSlideWindowMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister() : void {
			win.addEventListener(Event.CLOSE, onWindowClose);
			win.addEventListener(PhotoLoadEvent.LOAD_PHOTO_SUCCESS, showPhotoInfoWin);
			trace(this.multitonKey);
		}
		
		private function showPhotoInfoWin(e:PhotoLoadEvent):void {
			sendNotification(ShellFacade.TILE_PHOTO_LOAD_SUCCESS, e);
		}
		
		override public function listNotificationInterests():Array {
			return [PhotosSlideModuleFacade.GET_PHOTOS_SLIDE_WINDOW
					];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case PhotosSlideModuleFacade.GET_PHOTOS_SLIDE_WINDOW:
					sendNotification(PhotosSlideModuleFacade.EXPORT_PHOTOS_SLIDE_WINDOW, win);
					break;
			}
		}
		
		/**
		 * Handle window close.
		 * <P>
		 * Remove all references to the window that exist inside 
		 * the module, then unregister this Mediator.</P>
		 * <P>
		 * Note that the main app has a listener on the component
		 * as well, which it will use to remove it from the view 
		 * hierarchy and then will remove all refs in the app 
		 * itself.</P> 
		 * <P>
		 * After that the module instance and all its contents 
		 * should be garbage collected.</P>
		 */
		private function onWindowClose(e:Event):void {
			win.removeEventListener(Event.CLOSE, onWindowClose);
			setViewComponent(null);
			facade.removeMediator(this.getMediatorName());
		}
		
		private function get win():PhotosSlideWindow {
			return viewComponent as PhotosSlideWindow;
		}
	}
}